﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    public class DS_json : IDS
    {
        public override void save(MyList a)
        {
            using (StreamWriter sw = File.AppendText(path))
            {
                foreach (items pp in a)
                {
                    sw.WriteLine(pp.SaveToJSON());
                }
                sw.Close();
            }
        }
        public override IDS IsReady(string Fname)
        {
           
            path = Fname;
            IDS ret = new DS_json();
            string[] ext = path.Split('.');
            if (ext[1] == "json")
            {
                ret = this;
            }
            else
            {
                if (!(next == null)) ret = next.IsReady(Fname);
            }
            return ret;
        }
        public override MyList load()
        {
            MyList a = new MyList();
            using (StreamReader sr = File.OpenText(path))
            {
                string str;
                while ((str = sr.ReadLine()) != null)
                {
                    string[] split = str.Split(new Char[] { ':', '"', ',', '}' });
                    items type = Factory.CreateObject(split[1]);
                    type.LoadFromJSON(split);
                    a.Add(type);
                }
                sr.Close();
                return a;
            }
        }
    }
}
